package com.quanyue.music.system.mapper;

import com.quanyue.music.domain.constant.FMChannel;
import com.quanyue.music.domain.entity.FmNames;
import com.quanyue.music.domain.entity.FmVip;
import com.quanyue.music.domain.entity.PlayList;
import com.quanyue.music.system.type.*;
import com.shzisg.mybatis.mapper.auto.AutoMapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface PlayListMapper extends AutoMapper <PlayList> {
    @Select("select count(1) from qy_play_list pl "
            + "where DATE_FORMAT(pl.start_time,'%Y-%m-%d') = #{date} and pl.channel_num = #{channelNum}")
    Integer findCountByDate(@Param("date") LocalDate date,
                            @Param("channelNum") FMChannel fmChannel);

    @Select("select pl.*, f.title as music_title, f.status as fm_status, f.key_id "
            + "from qy_play_list pl "
            + "left join qy_fm f on pl.fm_music = f.id "
            + "where DATE_FORMAT(pl.start_time,'%Y-%m-%d') = #{date} and pl.channel_num = #{channelNum} "
            + "order by pl.start_time")
    List <FMPlayList> findMusicByDate(@Param("date") LocalDate date,
                                      @Param("channelNum") FMChannel fmChannel);

    @Select("select f.title, f.key_id from qy_play_list pl "
            + "left join qy_fm f on pl.fm_advertise = f.id "
            + "where pl.id = #{id}")
    BillInfo findBillByDate(@Param("id") String id);

    @Select("select pl.id, pl.fm_advertise, pl.fm_music, f.type as type_name, t.tag as tag_name, r.id as like_id, f.id as music_id, "
            + "f.fm_file, f.title, f.singer_name, f.description, f.like_count, pl.start_time "
            + "from qy_play_list pl "
            + "left join qy_fm f on f.id = pl.fm_music "
            + "left join qy_music_tag t on t.id = f.tag "
            + "left join qy_user_like r on r.fm_id = pl.id "
            + "and r.user_id = #{userId} and r.del_flag = false "
            + "where pl.start_time <= #{dateTime} and pl.end_time > #{dateTime} "
            + "and pl.channel_num = #{channelNum}")
    FMPlayDetail findPlayByDate(@Param("dateTime") LocalDateTime dateTime,
                                @Param("channelNum") FMChannel channelNum,
                                @Param("userId") String userId);

    @Select("select f.type as type_name, t.tag as tag_name, f.id as music_id, f.image, "
            + "f.fm_file, f.title, f.singer_name, f.description, f.like_count "
            + "from qy_fm f "
            + "left join qy_music_tag t on t.id = f.tag "
            + "where f.type = 'SOS'")
    FMPlayDetail findSos();

    @Select("select pl.id, f.type as type_name, f.fm_file, f.title, f.description, f.id as music_id "
            + "from qy_play_list pl "
            + "left join qy_fm f on f.id = pl.fm_advertise "
            + "where pl.id = #{id}")
    FMPlayDetail findPlayById(@Param("id") String id);

    @Select("select f.key_id, f.title "
            + "from qy_play_list pl "
            + "left join qy_fm f on f.id = pl.fm_music "
            + "where pl.start_time <= #{dateTime} and pl.end_time > #{dateTime} "
            + "and pl.channel_num = #{channelNum}")
    FMPlayRecord findCenterPlay(@Param("dateTime") LocalDateTime dateTime,
                                @Param("channelNum") FMChannel channelNum);

    PlayList findByTimeAndNum(@Param("startTime") LocalDateTime startTime,
                              @Param("channelNum") FMChannel channelNum);

    @Select("select * from qy_play_list pl "
            + "where pl.start_time <= #{dateTime} and pl.end_time > #{dateTime}")
    List <PlayList> findByTime(@Param("dateTime") LocalDateTime dateTime);

    @Select("select pl.channel_num, f.image from qy_play_list pl "
            + "left join qy_fm f on f.id = pl.fm_music "
            + "where pl.start_time <= #{dateTime} and pl.end_time > #{dateTime}")
    List <FMPlayImages> selectByTime(@Param("dateTime") LocalDateTime dateTime);

    @Select("select pl.channel_num, f.image from qy_play_list pl "
            + "left join qy_fm f on f.id = pl.fm_advertise "
            + "where pl.start_time <= #{dateTime} and pl.end_time > #{dateTime}")
    List <FMPlayImages> selectByTimeBill(@Param("dateTime") LocalDateTime dateTime);


    @Select("select * from qy_fm_vip where user_id = #{userId} and del_flag = false")
    FmVip findVipByUserId(@Param("userId") String userId);

    @Select("select * from qy_fm_names where fm_id = #{fmId} order by seconds")
    List <FmNames> findFmNames(@Param("fmId") String fmId);

    @Select("select id from qy_fm where key_id = #{keyId}")
    List <String> findByFmKeyId(@Param("keyId") String keyId);

    @Delete("delete from qy_play_list where start_time>=#{startTime} and start_time<=#{endTime} and channel_num=#{channel} ")
    void clearPlay(@Param("startTime") LocalDateTime startTime,
                   @Param("endTime") LocalDateTime endTime,
                   @Param("channel") FMChannel fmChannel);
}
